﻿using System;
using System.Windows.Input;
using GalaSoft.MvvmLight.Command;
using MSH.Common.Classes;
using WP7Client.Helpers;
using WP7Client.ServerManager;
using MSH.Common.DAO.Results;

namespace WP7Client.Controller
{
    public class ResetPasswordController : BaseController
    {
        #region Members

        private String _Username;
        private String _Password;
        private String _Password2;
        private String _MobilePhoneNumber;

        #endregion

        #region Get&Set

        public ICommand ResetCommand 
        { 
            get; 
            internal set; 
        }

        public String Username
        {
            get { return _Username; }
            set { _Username = value; }
        }

        public String Password
        {
            get { return _Password; }
            set { _Password = value; }
        }

        public String Password2
        {
            get { return _Password2; }
            set { _Password2 = value; }
        }

        public String MobilePhoneNumber
        {
            get { return _MobilePhoneNumber; }
            set { _MobilePhoneNumber = value; }
        }


        #endregion

        public ResetPasswordController()
        {
            ResetCommand = new RelayCommand(ResetExecute);
        }

        /// <summary>
        /// Aanvraag voor het wijzigen van het wachtwoord
        /// </summary>
        private void ResetExecute()
        {
            if (String.IsNullOrEmpty(Username) || String.IsNullOrEmpty(Password) || String.IsNullOrEmpty(Password2) || String.IsNullOrEmpty(MobilePhoneNumber))
            {
                GlobalHelper.ShowMessage("Error", "All fields need to be filled.");
                return;
            } else if(!Password.Equals(Password2))
            {
                GlobalHelper.ShowMessage("Error", "Password confirmation doesn't match your new password.");
                return;
            }

            Loading = true;
            HttpGet resetHttpGet = new HttpGet(String.Format(GlobalHelper.Url + "ChangePasswordRequest/{0}/{1}/{2}", Username, GlobalHelper.MD5(Password), MobilePhoneNumber), typeof(ChangePasswordRequestResult));
            resetHttpGet.FinishedGet += resetHttpGet_FinishedGet;
        }

        void resetHttpGet_FinishedGet(object sender, EventArgs e)
        {
            try
            {
                HttpGet get = sender as HttpGet;
                if (get == null || get.resultObject == null)
                    return; //Eventueel foutmelding

                ChangePasswordRequestResult result = (ChangePasswordRequestResult)get.resultObject;

                switch (result.Result)
                {
                    case ChangePasswordRequestResultEnum.UserNotExists:
                        GlobalHelper.ShowMessage("Error", "User doesn't exist. \nPlease try again.");
                        break;
                    case ChangePasswordRequestResultEnum.Succes:
                        GlobalHelper.ShowMessage("Information", "Your activation code has been sent to your phone.");
                        NavigationHelper.NavigateTo("/UserInterface/Pages/ActivateNewPasswordPage.xaml");
                        break;
                    case ChangePasswordRequestResultEnum.ToManyRequests:
                        GlobalHelper.ShowMessage("Error", "Maximum number of requests reached. \nPlease try to activate with your last received activation code.");
                        NavigationHelper.NavigateTo("/UserInterface/Pages/LoginPage.xaml");
                        break;
                    case ChangePasswordRequestResultEnum.IncorrectMobilePhoneNumber:
                        GlobalHelper.ShowMessage("Error", "Invalid mobile phone number and/or username.");
                        break;
                    case ChangePasswordRequestResultEnum.SamePassword:
                        GlobalHelper.ShowMessage("Error", "No need to request a new password, you just entered your current password!");
                        break;
                }
            }
            finally
            {
                Loading = false;
            }
        }
    }
}
